#include "stdafx.h"
#include "CppUnitTest.h"

#include "MxPluginManager\MxPluginManager1.h"
#include "PublicInterfaces\{FAFD3CB8-FCF3-46EE-B32E-45A4FF68073E}\IImageComputePluginLib.h"
#include "PublicInterfaces\{FAFD3CB8-FCF3-46EE-B32E-45A4FF68073E}\IImageCompute.h"
#include "PublicInterfaces\{FAFD3CB8-FCF3-46EE-B32E-45A4FF68073E}\IImageComputeFnDef.h"
#include "PublicInterfaces\{FAFD3CB8-FCF3-46EE-B32E-45A4FF68073E}\IImageComputeFn.h"

#include "..\..\PublicInterfaces\PluginICPopenCV\OCV-Dev-ProductID.h"

using namespace Microsoft::VisualStudio::CppUnitTestFramework;

namespace PluginICPopenCVTest
{
	TEST_CLASS(FloodfillImageTest)
	{
	private:
		MxPluginManager			*_pluginMngr;
		MxPluginLib				*_plugin;
		IImageComputePluginLib	*_lib;
		IImageCompute		    *_icp;
		IImageComputeFnDef		*_fnDef;
		IImageComputeFn			*_fn;

	public:

		TEST_METHOD_INITIALIZE(FloodfillImgInit)
		{
			_pluginMngr = new MxPluginManager();
			if (_pluginMngr->RefreshPluginsInRootFolder(SID_ImageProc, true) == true)
			{
				if ((_plugin = _pluginMngr->GetPlugin(PRODID_PluginICPopenCV)) != nullptr)
				{
					if ((_lib = static_cast<IImageComputePluginLib *>(_plugin->CreateClassInstance(IImageComputePluginLibIID::Guid()))) != nullptr)
					{
						if((_icp = _lib->GetIImageCompute()) != nullptr)
						{
							GUID fnID = MxGUID::ConvertFromString("{18F4AC4F-B042-45F1-A67E-0644BF301A48}");
							_fnDef =_icp->GetIImageComputeFnDef(fnID);
							_fn =_icp->GetIImageComputeFn(fnID);
						}
					}
				}
			}
		}

		TEST_METHOD_CLEANUP(FloodfillImgDeinit)
		{
			if(_fn != nullptr)
				_fn->DestroyPtr();
			_fn = nullptr;

			if(_fnDef!= nullptr)
				_fnDef->DestroyPtr();
			_fnDef = nullptr;

			if(_icp != nullptr)
				_icp->DestroyPtr();
			_icp = nullptr;

			if(_lib != nullptr)
				_lib->DestroyPtr();
			_lib = nullptr;

			if (_pluginMngr != nullptr)
				delete _pluginMngr;
			_pluginMngr = nullptr;
		}
		
		TEST_METHOD(FloodfillImgCreateTest)
		{
			Assert::IsNotNull(_fnDef);
			Assert::IsTrue(_fnDef->IsValid());

			Assert::IsNotNull(_fn);
			Assert::IsTrue(_fn->IsValid());
		}

		TEST_METHOD(FloodfillImgFnDefParamsTest)
		{
			Assert::IsNotNull(_fnDef);
			Assert::IsTrue(_fnDef->IsValid());

			Assert::AreEqual("FloodfillImg", _fnDef->GetLabel());
			Assert::AreEqual("Fills a connected component with the given color", _fnDef->GetDescription());
			Assert::AreEqual("Common", _fnDef->GetToolboxClass());
			Assert::AreEqual("Floodfill", _fnDef->GetToolboxLabel());
			Assert::AreEqual(17, _fnDef->GetToolboxIconClassOffset());

			Assert::AreEqual(8, _fnDef->GetParamCount(IImageComputeFnDef::Direction::Input));
			Assert::AreEqual(0, _fnDef->GetParamCount(IImageComputeFnDef::Direction::Output));
			Assert::AreEqual(2, _fnDef->GetParamCount(IImageComputeFnDef::Direction::IO));

			Assert::AreEqual("StopProc", _fnDef->GetParamName(IImageComputeFnDef::Direction::IO, 0));
			Assert::AreEqual("ImageRef", _fnDef->GetParamName(IImageComputeFnDef::Direction::IO, 1));
			Assert::AreEqual("RootFolder", _fnDef->GetParamName(IImageComputeFnDef::Direction::Input, 0));
			Assert::AreEqual("IJC", _fnDef->GetParamName(IImageComputeFnDef::Direction::Input, 1));
			Assert::AreEqual("IJF", _fnDef->GetParamName(IImageComputeFnDef::Direction::Input, 2));
			Assert::AreEqual("SeedPointX", _fnDef->GetParamName(IImageComputeFnDef::Direction::Input, 3));
			Assert::AreEqual("SeedPointY", _fnDef->GetParamName(IImageComputeFnDef::Direction::Input, 4));
			Assert::AreEqual("NewColorRed", _fnDef->GetParamName(IImageComputeFnDef::Direction::Input, 5));
			Assert::AreEqual("NewColorGreen", _fnDef->GetParamName(IImageComputeFnDef::Direction::Input, 6));
			Assert::AreEqual("NewColorBlue", _fnDef->GetParamName(IImageComputeFnDef::Direction::Input, 7));

			Assert::IsTrue(IImageComputeFnDef::Direction::IO == _fnDef->GetParamDirection("StopProc"));
			Assert::IsTrue(IImageComputeFnDef::Direction::Input == _fnDef->GetParamDirection("RootFolder"));
			Assert::IsTrue(IImageComputeFnDef::Direction::Input == _fnDef->GetParamDirection("IJC"));
			Assert::IsTrue(IImageComputeFnDef::Direction::Input == _fnDef->GetParamDirection("IJF"));
			Assert::IsTrue(IImageComputeFnDef::Direction::IO == _fnDef->GetParamDirection("ImageRef"));
			Assert::IsTrue(IImageComputeFnDef::Direction::Input == _fnDef->GetParamDirection("SeedPointX"));
			Assert::IsTrue(IImageComputeFnDef::Direction::Input == _fnDef->GetParamDirection("SeedPointY"));
			Assert::IsTrue(IImageComputeFnDef::Direction::Input == _fnDef->GetParamDirection("NewColorRed"));
			Assert::IsTrue(IImageComputeFnDef::Direction::Input == _fnDef->GetParamDirection("NewColorGreen"));
			Assert::IsTrue(IImageComputeFnDef::Direction::Input == _fnDef->GetParamDirection("NewColorBlue"));

			Assert::IsTrue(_fnDef->ConvertStrToParamType(_fnDef->GetParamValType("StopProc")) == IImageComputeFnDef::ParamType::Bool);
			Assert::IsTrue(_fnDef->ConvertStrToParamType(_fnDef->GetParamValType("RootFolder")) == IImageComputeFnDef::ParamType::String);
			Assert::IsTrue(_fnDef->ConvertStrToParamType(_fnDef->GetParamValType("IJC")) == IImageComputeFnDef::ParamType::String);
			Assert::IsTrue(_fnDef->ConvertStrToParamType(_fnDef->GetParamValType("IJF")) == IImageComputeFnDef::ParamType::String);
			Assert::IsTrue(_fnDef->ConvertStrToParamType(_fnDef->GetParamValType("ImageRef")) == IImageComputeFnDef::ParamType::String);
			Assert::IsTrue(_fnDef->ConvertStrToParamType(_fnDef->GetParamValType("SeedPointX")) == IImageComputeFnDef::ParamType::Integer);
			Assert::IsTrue(_fnDef->ConvertStrToParamType(_fnDef->GetParamValType("SeedPointY")) == IImageComputeFnDef::ParamType::Integer);
			Assert::IsTrue(_fnDef->ConvertStrToParamType(_fnDef->GetParamValType("NewColorRed")) == IImageComputeFnDef::ParamType::Integer);
			Assert::IsTrue(_fnDef->ConvertStrToParamType(_fnDef->GetParamValType("NewColorGreen")) == IImageComputeFnDef::ParamType::Integer);
			Assert::IsTrue(_fnDef->ConvertStrToParamType(_fnDef->GetParamValType("NewColorBlue")) == IImageComputeFnDef::ParamType::Integer);

			Assert::AreEqual(_fnDef->ConvertParamTypeToStr(IImageComputeFnDef::ParamType::String), _fnDef->GetParamValType("RootFolder"));

			Assert::IsFalse(_fnDef->IsParamValMinSet("RootFolder"));
			Assert::IsFalse(_fnDef->IsParamValMaxSet("RootFolder"));
			Assert::IsFalse(_fnDef->IsParamValMinSet("IJC"));
			Assert::IsFalse(_fnDef->IsParamValMaxSet("IJC"));
			Assert::IsFalse(_fnDef->IsParamValMinSet("IJF"));
			Assert::IsFalse(_fnDef->IsParamValMaxSet("IJF"));
			Assert::IsFalse(_fnDef->IsParamValMinSet("ImageRef"));
			Assert::IsFalse(_fnDef->IsParamValMaxSet("ImageRef"));
			Assert::IsTrue(_fnDef->IsParamValMinSet("SeedPointX"));
			Assert::IsFalse(_fnDef->IsParamValMaxSet("SeedPointX"));
			Assert::IsTrue(_fnDef->IsParamValMinSet("SeedPointY"));
			Assert::IsFalse(_fnDef->IsParamValMaxSet("SeedPointY"));
			Assert::IsTrue(_fnDef->IsParamValMinSet("NewColorRed"));
			Assert::IsFalse(_fnDef->IsParamValMaxSet("NewColorRed"));
			Assert::IsTrue(_fnDef->IsParamValMinSet("NewColorGreen"));
			Assert::IsFalse(_fnDef->IsParamValMaxSet("NewColorGreen"));
			Assert::IsTrue(_fnDef->IsParamValMinSet("NewColorBlue"));
			Assert::IsFalse(_fnDef->IsParamValMaxSet("NewColorBlue"));

			Assert::AreEqual(0, _fnDef->GetParamValMin("SeedPointX"));
			Assert::AreEqual(0, _fnDef->GetParamValMin("SeedPointY"));
			Assert::AreEqual(0, _fnDef->GetParamValMin("NewColorRed"));
			Assert::AreEqual(0, _fnDef->GetParamValMin("NewColorGreen"));
			Assert::AreEqual(0, _fnDef->GetParamValMin("NewColorBlue"));

			Assert::AreEqual("no", _fnDef->GetParamDefaultVal("StopProc"));
			Assert::AreEqual("-", _fnDef->GetParamDefaultVal("RootFolder"));
			Assert::AreEqual("-", _fnDef->GetParamDefaultVal("IJC"));
			Assert::AreEqual("-", _fnDef->GetParamDefaultVal("IJF"));
			Assert::AreEqual("-", _fnDef->GetParamDefaultVal("ImageRef"));
			Assert::AreEqual("0", _fnDef->GetParamDefaultVal("SeedPointX"));
			Assert::AreEqual("0", _fnDef->GetParamDefaultVal("SeedPointY"));
			Assert::AreEqual("255", _fnDef->GetParamDefaultVal("NewColorRed"));
			Assert::AreEqual("255", _fnDef->GetParamDefaultVal("NewColorGreen"));
			Assert::AreEqual("255", _fnDef->GetParamDefaultVal("NewColorBlue"));

			Assert::AreEqual("Stop", _fnDef->GetParamLabel("StopProc"));
			Assert::AreEqual("Root folder", _fnDef->GetParamLabel("RootFolder"));
			Assert::AreEqual("IJC id", _fnDef->GetParamLabel("IJC"));
			Assert::AreEqual("IJF id", _fnDef->GetParamLabel("IJF"));
			Assert::AreEqual("ImageRef", _fnDef->GetParamLabel("ImageRef"));
			Assert::AreEqual("SeedPointX", _fnDef->GetParamLabel("SeedPointX"));
			Assert::AreEqual("SeedPointY", _fnDef->GetParamLabel("SeedPointY"));
			Assert::AreEqual("NewColorRed", _fnDef->GetParamLabel("NewColorRed"));
			Assert::AreEqual("NewColorGreen", _fnDef->GetParamLabel("NewColorGreen"));
			Assert::AreEqual("NewColorBlue", _fnDef->GetParamLabel("NewColorBlue"));

			Assert::AreEqual("Do not process this step or any further steps", _fnDef->GetParamDesc("StopProc"));
			Assert::AreEqual("folder for project root", _fnDef->GetParamDesc("RootFolder"));
			Assert::AreEqual("GUID for job collection", _fnDef->GetParamDesc("IJC"));
			Assert::AreEqual("GUID for job file", _fnDef->GetParamDesc("IJF"));
			Assert::AreEqual("name of input image in ImageFolder without filetype", _fnDef->GetParamDesc("ImageRef"));
			Assert::AreEqual("SeedPointX: X coordinate of seed point in range of 0 to image width", _fnDef->GetParamDesc("SeedPointX"));
			Assert::AreEqual("SeedPointY: Y coordinate of seed point in range of 0 to image height", _fnDef->GetParamDesc("SeedPointY"));
			Assert::AreEqual("NewColorRed: Red component of fill color in range of 0 to 255", _fnDef->GetParamDesc("NewColorRed"));
			Assert::AreEqual("NewColorGreen:  Green component of fill color in range of 0 to 255", _fnDef->GetParamDesc("NewColorGreen"));
			Assert::AreEqual("NewColorBlue:  Blue component of fill color in range of 0 to 255", _fnDef->GetParamDesc("NewColorBlue"));
		}

		TEST_METHOD(FloodfillImgRunTest)
		{
			Assert::IsNotNull(_fn);
			Assert::IsTrue(_fn->IsValid());

			Assert::IsTrue(_fn->InitCall(0, ".bmp", true, true));

			Assert::IsTrue(_fn->SetParamBool("StopProc", false));
			Assert::IsTrue(_fn->SetParamStr("RootFolder", "..\\..\\..\\TestImages"));
			Assert::IsTrue(_fn->SetParamStr("IJC", "IJC"));
			Assert::IsTrue(_fn->SetParamStr("IJF", "IJF"));
			Assert::IsTrue(_fn->SetParamStr( "ImageRef", "FloodfillTest"));
			Assert::IsTrue(_fn->SetParamInt("SeedPointX", 0));
			Assert::IsTrue(_fn->SetParamInt("SeedPointY", 0));
			Assert::IsTrue(_fn->SetParamInt("NewColorRed", 255));
			Assert::IsTrue(_fn->SetParamInt("NewColorGreen", 255));
			Assert::IsTrue(_fn->SetParamInt("NewColorBlue", 255));

			Assert::IsTrue(_fn->ValidateCall());
			Assert::IsTrue(_fn->DoCall());
		}

	};
}